﻿using ARPaintServer.Model.ObjectDetection;
using PaddleOCRSharp;

namespace ARPaintServer.Services
{
    public class OCRService : IOCRService
    {
        OCRModelConfig Config = null;
        OCRParameter oCRParameter = new OCRParameter();
        static PaddleOCREngine paddleOCREngine;

        public OCRService()
        {
            paddleOCREngine ??= new PaddleOCREngine(Config, oCRParameter);
        }

        public Task<ItemResult> GetTextFromBase64(string imageBase64, string imageName = "null")
        {
            OCRResult oCRResult = paddleOCREngine.DetectTextBase64(imageBase64);
            ItemResult itemResult = new() { ImageName = imageName, ImageItems = new() };

            foreach (var item in oCRResult.TextBlocks)
            {
                ItemInfo itemInfo = new()
                {
                    Label = item.Text,
                    Score = item.Score,
                    X = item.BoxPoints[0].X,
                    Y = item.BoxPoints[0].Y,
                };
                itemResult.ImageItems.Add(itemInfo);
            }
            return Task.FromResult(itemResult);

        }
    }
}
